package com.zslibrary.StateHandle;

import com.zslibrary.BusMaster.HandleError;
import com.zslibrary.Enum.NFCMessageId;
import com.zslibrary.Enum.StateDefine;
import com.zslibrary.Enum.StateEventDefine;
import com.zslibrary.Pipe.Pipe;
import com.zslibrary.Protocol.Poll;
import com.zslibrary.Tools.ZSLog;

public class NFCReconnectState extends StateHandle{
    private byte mReconnectTimes=0;
    private Pipe mPipe;
    private String mErrorCode;

    public NFCReconnectState(Pipe pipe,String errorCode){
        mPipe=pipe;
        mErrorCode=errorCode;
    }

    @Override
    public @StateEventDefine.StateEvent int handle(){
//        ZSLog.d("reconnect state");
        return Poll.NFCPoll(mPipe);
    }

    @Override
    public @StateDefine.State int getNextHop(@StateEventDefine.StateEvent int event) {
        if(event== StateEventDefine.EVT_NONE) {
            if(mReconnectTimes++>=4){
                mReconnectTimes=0;
                HandleError.clearError(mErrorCode);
                return StateDefine.STATE_IDLE;
            }
        }
        else if(event==StateEventDefine.EVT_RESET)
            return StateDefine.STATE_INIT;
        else if(event==StateEventDefine.EVT_DISCONNECT){
            mReconnectTimes=0;
            return StateDefine.STATE_ERROR;
        }
        return  StateDefine.STATE_RECONNECT;
    }
}
